<template>
  <div class="login_page fillcontain">
    <nav-menu :pageIndex="pageIndex"></nav-menu>
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer">
        <div class="manage_tip">
          <p>用户登录</p>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm">
          <el-form-item prop="username">
            <el-input v-model="loginForm.username" placeholder="用户名"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" placeholder="密码" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="submitForm('loginForm')" class="submit_btn">登录</el-button>
          </el-form-item>
        </el-form>
        <!--        <p class="tip">温馨提示：</p>-->
        <a href="./#/register" class="tip">立即注册</a>
      </section>
    </transition>
  </div>
</template>

<script>
import NavMenu from "@/components/NavMenu.vue";
import Vue from "vue";

export default {
  name: "Login",
  components: {
//    SearchLink,
    NavMenu,
  },
  data() {
    return {
      pageIndex: "1", //当前页的序号
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          {required: true, message: '请输入用户名', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入密码', trigger: 'blur'},
          // { min: 6, max: 16, message: '长度在 6 到 16 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  mounted() {
    // document.body.style.backgroundColor="#324057";
    this.init()
  },
  methods: {
    init() {
      let isLogin = false
      if (localStorage.getItem('userLoginStatus') === 'true') {
        isLogin = true
      } else {
        isLogin = false
      }
      if (isLogin) {
        this.$router.push('/')
      }
    },
    loginCheck(res) {
      console.log('Login res:', res)
      // 向后台验证账号是否登录成功，返回status，1代表登录成功，0代表失败
      // res.message 写明原因，或者其他原因

      // 注册成功自动跳转至登录界面
      if (res.data.retcode === 0) {
        this.$message({
          type: 'success',
          message: '登录成功'
        });
        localStorage.setItem('userLoginStatus', 'true')
        localStorage.setItem('userInfo', JSON.stringify(res.data.user))
        this.$router.push('/')
      } else {
        this.$message({
          type: 'error',
          message: '登录失败，请检查用户名或密码是否正确'
          // message: res.data.message
        });
      }
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // const res = await login({user_name: this.loginForm.username, password: this.loginForm.password})
          Vue.prototype.post('http://192.168.0.94:2226/login', {
            id: this.loginForm.username,
            pwd: this.loginForm.password
          }, this.loginCheck)
        } else {// 表单验证失败
          this.$notify.error({
            title: '错误',
            message: '请输入正确的用户名密码',
            offset: 100
          });
          return false;
        }
      });
    },
  }
}
</script>

<style lang="less" scoped>
@import '../style/mixin';

.login_page {
  background-color: #324057;
  height: 100%;
  width: 100%;
}

.manage_tip {
  position: absolute;
  width: 100%;
  top: -100px;
  left: 0;

  p {
    font-size: 34px;
    color: black;
  }
}

.form_contianer {
  border: 1px solid #ededed;
  .wh(320px, 210px);
  .ctp(320px, 210px);
  padding: 25px;
  border-radius: 5px;
  text-align: center;
  background-color: #fff;

  .submit_btn {
    width: 100%;
    font-size: 16px;
  }
}

.tip {
  font-size: 18px;
  color: blue;
  float: right;
  text-decoration-line: none
}

.form-fade-enter-active, .form-fade-leave-active {
  transition: all 1s;
}

.form-fade-enter, .form-fade-leave-active {
  transform: translate3d(0, -50px, 0);
  opacity: 0;
}
</style>
